package com.xxxx.order.controller;

import com.xxxx.api.pojo.Admin;
import com.xxxx.api.pojo.CartResult;
import com.xxxx.api.service.CartService;
import com.xxxx.api.utils.LoginUserUtil;
import com.xxxx.common.result.BaseResult;
import com.xxxx.order.service.OrderService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @Author Lionel
 * @Date 2020/11/6 13:16
 * Created by IntelliJ IDEA 2020.1.
 * Project: shop
 * Description：订单控制层
 */
@Controller
@RequestMapping("/order")
public class OrderController {

    @Reference(version = "1.0")
    private CartService cartService;
    @Resource
    private OrderService orderService;


    /**
     * 跳转预定单页面
     * @return
     */
    @RequestMapping("/preOrder")
    public String preOrder(Model model, HttpServletRequest request){
        int userId = LoginUserUtil.releaseUserIdFromCookie(request);
        Admin admin = new Admin();
        admin.setAdminId((short) userId);
        model.addAttribute("cartResult",cartService.getCartList(admin));
        return "order/preOrder";
    }

    /**
     * 跳转到订单提交页面
     * @return
     */
    @RequestMapping("/submitOrder")
    public String submitOrder(Model model,HttpServletRequest request){
        int userId = LoginUserUtil.releaseUserIdFromCookie(request);
        Admin admin = new Admin();
        admin.setAdminId((short) userId);

        CartResult cartResult = cartService.getCartList(admin);

        // 1.存入订单信息
        BaseResult baseResult = orderService.orderSave(admin, cartResult);
        //2.清除购物车信息
        cartService.cleanMyCart(admin);
        // 总价
        model.addAttribute("totalPrice",cartResult.getTotalPrice());
        //订单编号
        model.addAttribute("orderSn", baseResult.getMessage());
        //页面跳转
        return "order/submitOrder";
    }

}
